import 'package:flutter/material.dart';
import 'package:shared_preferences/shared_preferences.dart';
import 'package:fluttertoast/fluttertoast.dart';

import 'package:wanandroid_flutter/common/application.dart';
import 'package:wanandroid_flutter/config/default.config.dart';
import 'package:wanandroid_flutter/event/theme_change_event.dart';
import 'package:wanandroid_flutter/ui/about_app_page.dart';

class DrawerWidget extends StatefulWidget {
  _DrawerWidgetState createState() => _DrawerWidgetState();
}

class _DrawerWidgetState extends State<DrawerWidget> {
  @override
  Widget build(BuildContext context) {
    return Drawer(
      child: ListView(
        padding: EdgeInsets.zero,
        children: <Widget>[
          UserAccountsDrawerHeader(
            accountName: Text(
              'fw6',
              style: TextStyle(
                fontWeight: FontWeight.bold,
              ),
            ),
            accountEmail: Text('lyman.von9506@gmail.com'),
            currentAccountPicture: CircleAvatar(
              backgroundImage: NetworkImage(
                  'https://avatars2.githubusercontent.com/u/45113594?s=400&u=5e466021aca60dde1739dfd11a647b91aa80d73f&v=4'),
            ),
            decoration: BoxDecoration(
              color: Colors.cyan[400],
              image: DecorationImage(
                image: NetworkImage('https://fw6.github.io/images/banner2.jpg'),
                fit: BoxFit.cover,
                colorFilter:
                    ColorFilter.mode(Colors.cyan[600], BlendMode.hardLight),
              ),
            ),
          ),
          ListTile(
            title: Text(
              '夜间模式',
              textAlign: TextAlign.left,
            ),
            leading: Icon(
              Icons.wb_sunny,
              color: GlobalConfig.themeData.accentColor,
              size: 22.0,
            ),
            onTap: () {
              setState(() {
                if (GlobalConfig.dark == true) {
                  GlobalConfig.dark = false;
                } else {
                  GlobalConfig.dark = true;
                }

                changeTheme();
              });
            },
          ),
          ListTile(
            title: Text(
              '设置',
              textAlign: TextAlign.left,
            ),
            leading: Icon(
              Icons.settings,
              color: GlobalConfig.themeData.accentColor,
              size: 22.0,
            ),
            onTap: () {
              Fluttertoast.showToast(msg: '该功能暂未上线。');
            },
          ),
          ListTile(
            title: Text(
              '关于APP',
              textAlign: TextAlign.left,
            ),
            leading: Icon(
              Icons.perm_identity,
              color: GlobalConfig.themeData.accentColor,
              size: 22.0,
            ),
            onTap: () {
              onAboutClick();
              Scaffold.of(context).openEndDrawer();
            },
          ),
        ],
      ),
    );
  }

  void changeTheme() async {
    SharedPreferences sp = await SharedPreferences.getInstance();
    sp.setBool('ThemeIndex', GlobalConfig.dark);
    Application.eventBus.fire(ThemeChangeEvent(GlobalConfig.dark));
  }

  void onAboutClick() async {
    await Navigator.of(context).push(MaterialPageRoute(
      builder: (context) => AboutAppPage(),
    ));
  }
}
